package javaweb.sevice.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import javaweb.mapper.StudentMapper;
import javaweb.pojo.Clazz;
import javaweb.pojo.Student;
import javaweb.pojo.StudentPageResult;
import javaweb.pojo.StudentQueryParam;
import javaweb.sevice.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    /*
    * 获取班级信息
    * */
    @Override
    public List<Clazz> getClazzList() {
        return studentMapper.selectClazzList();
    }

    /*
    * 获取班级学员信息
    * */
    @Override
    public StudentPageResult findAllStudent(StudentQueryParam studentQueryParam) {
        PageHelper.startPage(studentQueryParam.getPage(), studentQueryParam.getPageSize());
        //调用Mapper方法
        List<Student> list = studentMapper.selectAllStudent(studentQueryParam);
        Page<Student> p = (Page<Student>) list;

        //封装StudentPageResult对象
        return new StudentPageResult(p.getTotal(), p.getResult());

    }

    /*
    * 新增学员操作
    * */
    @Override
    public void addStudent(Student student) {
        //设置基本信息
        short count = 0;
        short score = 0;

        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        student.setViolationCount(count);
        student.setViolationScore(score);
        studentMapper.insertStudent(student);
    }

    /*
    * 根据id查询学员信息
    * */
    @Override
    public Student findStudentById(int id) {
        return studentMapper.selectStudentById(id);
    }

    /*
    * 修改学员的信息
    * */
    @Override
    public void updateStudent(Student student) {
        student.setUpdateTime(LocalDateTime.now());

        studentMapper.updateStudent(student);
    }

    /*
    * 批量删除学员信息
    * */
    @Override
    public void deleteStudentById(Integer[] ids) {
        /*
        * 批量删除学员
        * */
        if(!CollectionUtils.isEmpty(List.of(ids))){
                studentMapper.deleteStudentById(ids);
        }
    }

    /*
    * 学员违纪处理
    * */
    @Override
    public void disciplinaryStudent(Integer id, Integer score) {
        //修改学员信息
        Student student = studentMapper.selectStudentById(id);

        student.setUpdateTime(LocalDateTime.now());
        student.setViolationCount((short) (student.getViolationCount() + 1));
        student.setViolationScore((short) (student.getViolationScore() + score));

        studentMapper.updateStudentDisciplinary(student);
    }
}
